package com.iot08.admin.job;

import com.iot08.admin.service.ChannelService;
import com.iot08.admin.service.DeviceModelsService;
import com.iot08.admin.service.EntryRecordService;
import com.iot08.admin.service.VideoModelService;
import com.iot08.admin.utils.RedisEnum;
import com.iot08.common.config.redis.RedisService;
import com.iot08.common.entity.app.DeviceModels;
import com.iot08.common.utils.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;


/**
 * @version 1.0
 * @Description: 定时器相关
 * @time: 2020/9/8 11:47
 * @author: Rock
 */
@Slf4j
@Lazy(false)
@Component
public class ScheduledJob {

    @Resource
    private ChannelService channelService;
    @Resource
    private EntryRecordService entryRecordService;
    @Resource
    private VideoModelService videoModelService;

    @Resource
    private DeviceModelsService deviceModelsService;
    @Resource
    private RedisService redisService;

    /**
     * 更新设备数量相关定时器
     *
     * @time: 2020/9/8 11:53
     * @author: Rock
     * @version 1.0
     */
    @Scheduled(cron = "0 0 0/1 * * ?")
    public void updateDeviceNumJob() {
        log.info("updateDeviceNumJob start!!");
        long start = System.currentTimeMillis();
        channelService.updateNumJop();
        long channelTime = System.currentTimeMillis();
        log.info("channelService.updateNumJop() time = {}", channelTime - start);
        entryRecordService.updateNumJop();
        long entryRecordTime = System.currentTimeMillis();
        log.info("entryRecordService.updateNumJop() time = {}", entryRecordTime - channelTime);
        videoModelService.updateImportNumJob();
        long videoModelTime = System.currentTimeMillis();
        log.info("videoModelService.updateImportNumJob() time = {}", videoModelTime - entryRecordTime);
        deviceModelsService.updateNumJop();
        long deviceModelsTime = System.currentTimeMillis();
        log.info("deviceModelsService.updateNumJop() time = {}", deviceModelsTime - videoModelTime);
        redisService.set(RedisEnum.DeviceTotalTime.getKey(), DateUtil.dateToStr(new Date(), DateUtil.FORMAT_1));
        log.info("updateDeviceNumJob end!!");
    }

}
